package cn.dceast.former.service;

import cn.dceast.common.utils.ShellUtils;
import cn.dceast.former.dao.EyeImgMapper;
import cn.dceast.former.model.EyeImg;
import com.jcraft.jsch.JSchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * Created by zhouxiaa on 2017-07-18.
 * 结果展示页面
 */
@Service
public class EyeImgSettingService {

    @Autowired
    private EyeImgMapper eyeImgSettingMapper;


    public int addSetting(EyeImg eyeImg) {
        return eyeImgSettingMapper.insertSelective(eyeImg);
    }

    public int updateSetting(EyeImg eyeImg) {
        return eyeImgSettingMapper.updateByPrimaryKey(eyeImg);
    }

    public int deleteSetting(Integer id) {
        return eyeImgSettingMapper.deleteByPrimaryKey(id);
    }

    public List<Map> selectAllSetting(Integer modelId) {
        return eyeImgSettingMapper.selectAllSetting(modelId);
    }
    public EyeImg selectSettingById(Integer id) {
        return eyeImgSettingMapper.selectByPrimaryKey(id);
    }

    public List<Map> getImgDataList(Integer modelId, String tableName) {
        //查询出modelId相应的图形设置List
        List<Map> ImgList = eyeImgSettingMapper.selectAllSetting(modelId);
        HashMap params = new HashMap();
        List<Map> dataList = new ArrayList<>();
        List<Map> yList = null;
        String xColumn = "";
        Integer imgId = null;
        String yColumn = "";
        String yValue = "";
        //循环生成图形相关数据
        for(int i = 0; i<ImgList.size();i++){
            xColumn = (String)ImgList.get(i).get("xColumn");
            imgId = (Integer)ImgList.get(i).get("imgId");
            yColumn = (String)ImgList.get(i).get("yColumn");
            yValue = (String)ImgList.get(i).get("yValue");
            params.put("xColumn",xColumn);
            params.put("yColumn",yColumn);
            params.put("tableName",tableName);
            //0-求和，1-计数，2-原始值
            if(yValue.equals("0")){
                yList = getSum(params);
            }else if(yValue.equals("1")){
                yList = getCount(params);
            }else if(yValue.equals("2")){
                yList = getData(params);
            }

            //1-柱状图，2-折线图，3-饼图，4-散点图
            if(imgId==1){
                Map resultMap = new HashMap();
                resultMap.put(imgId,changeZhu(yList));
                resultMap.put("imgName",(String)ImgList.get(i).get("name"));
                dataList.add(resultMap);
            }else if(imgId==2){
                Map resultMap = new HashMap();
                resultMap.put(imgId,changeZhe(yList));
                resultMap.put("imgName",(String)ImgList.get(i).get("name"));
                dataList.add(resultMap);
            }else if(imgId==3){
                Map resultMap = new HashMap();
                resultMap.put(imgId,changeBing(yList));
                resultMap.put("imgName",(String)ImgList.get(i).get("name"));
                dataList.add(resultMap);
            }else if(imgId==4){
                Map resultMap = new HashMap();
                resultMap.put(imgId,changeSan(yList));
                resultMap.put("imgName",(String)ImgList.get(i).get("name"));
                dataList.add(resultMap);
            }
        }
        return dataList;
    }

    private List<Map> changeSan(List<Map> yList) {
        List<Map> list = new ArrayList<>();
        Object[] stryArr = new Object[yList.size()];
        for(int i=0;i<yList.size();i++){
            stryArr[0] =  yList.get(i).get("xColumn");
            stryArr[1] =  yList.get(i).get("yColumn");
        }
        Map map = new HashMap();
        map.put("series_data",stryArr);
        list.add(map);
        return list;
    }

    private List<Map> changeZhe(List<Map> yList) {
        List<Map> list = new ArrayList<>();
        List<Map> arraylist = new ArrayList<>();
        Object[] strLenArr = new Object[yList.size()];
        Object[] stryLenArr = new Object[yList.size()];
        for(int i=0;i<yList.size();i++){
            stryLenArr[i] = yList.get(i).get("yColumn");
            strLenArr[i] =  yList.get(i).get("xColumn");
        }
        Map map = new HashMap();
        map.put("type","line");
        map.put("data", stryLenArr);
        arraylist.add(map);
        Map resultMap = new HashMap();
        resultMap.put("xAxis_data",strLenArr);
        resultMap.put("series_data",arraylist);
        list.add(resultMap);
        return list;
    }

    private List<Map> changeBing(List<Map> yList) {
        List<Map> list = new ArrayList<>();
        List<Map> valueList = new ArrayList<>();
        Object[] strLenArr = new Object[yList.size()];
        for(int i=0;i<yList.size();i++){
            Map map = new HashMap();
            map.put("name", yList.get(i).get("xColumn"));
            map.put("value", yList.get(i).get("yColumn"));
            strLenArr[i] =  yList.get(i).get("xColumn");
            valueList.add(map);
        }
        Map resultMap = new HashMap();
        resultMap.put("legend_data",strLenArr);
        resultMap.put("series_data",valueList);
        list.add(resultMap);
        return list;
    }

    private List<Map> changeZhu(List<Map> yList) {
        List<Map> list = new ArrayList<>();
        Object[] strxArr = new String[yList.size()];
        Object[] stryArr = new Object[yList.size()];
        for(int i=0;i<yList.size();i++){
            strxArr[i] = yList.get(i).get("xColumn");
            stryArr[i] = yList.get(i).get("yColumn");
        }
        Map map = new HashMap();
        map.put("xAxis_data",strxArr);
        map.put("series_data",stryArr);
        list.add(map);
        return list;
    }

    private List<Map> getData(HashMap params) {
        List<Map> list = eyeImgSettingMapper.getData(params);
        return list;
    }

    private List<Map> getCount(HashMap params) {
        List<Map> list = eyeImgSettingMapper.getCount(params);
        return list;
    }

    private List<Map> getSum(HashMap map) {
        List<Map> list = eyeImgSettingMapper.getSum(map);
        return list;
    }

    public int checkHaveSettingName(EyeImg eyeImg) {
        return eyeImgSettingMapper.checkHaveSettingName(eyeImg);
    }

    public int checkHaveSameName(EyeImg eyeImg) {
        return eyeImgSettingMapper.checkHaveSameName(eyeImg);
    }
}
